package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.domain.db.PageResult;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import com.tanhua.dubbo.mapper.UserMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

@Service//启动的时候自动注册
public class UserInfoApiImpl implements UserInfoApi {

    //注入UserMapper对象
    @Autowired
    UserInfoMapper userInfoMapper;

    /**
     * 保存用户基本信息
     * @param userInfo
     * @return
     */
    @Override
    public int saveInfo(UserInfo userInfo) {
        return userInfoMapper.insert(userInfo);
    }

    /**
     * 更新用户基本信息
     * @param userInfo
     * @return
     */
    @Override
    public int updateInfo(UserInfo userInfo) {
        return userInfoMapper.updateById(userInfo);
    }

    /**
     * 根据ID查询用户
     */
    @Override
    public UserInfo findUserInfoByID(Long id) {
        return userInfoMapper.selectById(id);
    }

    /**
     * 用户数据翻页
     */
    @Override
    public PageResult<UserInfo> findUserInfoPage(Integer page, Integer pagesize) {
        //设置分页条件
        IPage  page1 = new Page<>(page, pagesize);
        //调用方法 传入page对象
        IPage iPage = userInfoMapper.selectPage(page1, null);
        //从iPage对象提取数据 封装PageResult<UserInfo>对象
        PageResult<UserInfo> userInfoPageResult = new PageResult<>();
        userInfoPageResult.setPage(page);
        userInfoPageResult.setPagesize(pagesize);
        userInfoPageResult.setCounts((int)iPage.getTotal());
        userInfoPageResult.setPages((int)iPage.getPages());
        userInfoPageResult.setItems(iPage.getRecords());
        return userInfoPageResult;
    }


}
